<?php

namespace app\admin\controller;

use app\admin\model\User as UserModel;
use app\admin\model\Role as RoleModel;
use app\BaseController;
use think\facade\View;

class User extends BaseController
{
    public function index()
    {
        $roles = RoleModel::select();
        View::assign('roles', $roles);
        return view();
    }

    public function list_data()
    {
        $users = new UserModel;
        $limit = input('limit');
        $users = $users->paginate($limit);
        return lay_data($users);
    }

    public function save()
    {
        try {
            $data  = $this->request->only(['username', 'password', 'name', 'role_id']);
            $check = UserModel::withTrashed()->where('username', $data['username'])->count();
            $check_2 = UserModel::withTrashed()->where('name', $data['name'])->count();
            if ($check || $check_2) {
                return msg_error('用户名或昵称重复');
            }
            $data['password'] = encrypt($data['password'], $data['username']);
            $user             = (new UserModel)->createAndLog($data);

            return return_true_or_false($user);

        } catch (\Exception $e) {
            return msg_error($e->getMessage());
        }
    }


    public function update($id)
    {
        try {
            $field = $this->request->only(['username', 'password', 'name', 'role_id']);

            $check_2 = UserModel::withTrashed()->where('name', $field['name'])->find();
            if ($check_2 && $id != $check_2->id) {
                return msg_error('昵称已被占用');
            }

            $user = UserModel::find($id);
            if (!$user) {
                return msg_error('账号不存在');
            }
            if ($field['password']) {
                $user->password = encrypt($field['password'], $user->username);
            } else {
                unset($field['password']);
            }
            $user->name    = $field['name'];
            $user->role_id = $field['role_id'];
            $info = $user->saveAndLog();

            return return_true_or_false($info);

        } catch (\Exception $e) {
            return msg_error($e->getMessage());
        }
    }


    public function delete($id)
    {
        try {
            $user     = UserModel::find($id);
            $session  = session('token');
            $username = decrypt($session);
            if ($user->username == $username) {
                return msg_error('不能删除自己');
            }
            if ($user->username == 'admin') {
                return msg_error('不能删除超级管理员');
            }
            $info = $user->deleteAndLog();
            return return_true_or_false($info);
        } catch (\Exception $e) {
            return msg_error($e->getMessage());
        }
    }

    public function update_password()
    {
        try {
            $data = $this->request->only(['password', 're_password', 'old_password']);
            if ($data['password'] !== $data['re_password']) {
                return msg_error('两次密码不一样');
            }
            $user = auth();
            $encrypt = encrypt($data['old_password'], $user->username);
            if($user->password !== $encrypt){
                return msg_error('当前密码错误');
            }
            $user->password = encrypt($data['password'],$user->username);
            $info = $user->saveAndLog();

            return return_true_or_false($info,'修改成功');
        } catch (\Exception $e) {
            return msg_error($e->getMessage());
        }
    }
    public function read($id)
    {
        $user = auth();
        if($id == $user->id){
            return view();
        }else{
            return '';
        }
    }
    public function update_info($id)
    {
        try {
            $field = $this->request->only(['name', 'old_password', 'password', 're_password']);
            $now_user = auth();
            if($id != $now_user->id){
                return msg_error('只能修改自己的信息');
            }
            $user = UserModel::find($id);
            if (!$user) {
                return msg_error('账号不存在');
            }
            if ($field['password'] !== $field['re_password']) {
                return msg_error('两次密码不一样');
            }
            $encrypt = encrypt($field['old_password'], $user->username);
            if($user->password !== $encrypt){
                return msg_error('当前密码错误');
            }
            $user->password = encrypt($field['password'],$user->username);
            $user->name = $field['name'];
            $info = $user->saveAndLog();
            return return_true_or_false($info,'修改成功');
        } catch (\Exception $e) {
            return msg_error($e->getMessage());
        }
    }
}